<?php

echo "Processing job\n";

$username="root";
$password="dwd4zx6p";
$database="docdiscuss";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database\n");

echo "Connected to database.. Retrieving next job..\n";

$query = "SELECT * FROM documents WHERE status = '0' ORDER BY id ASC LIMIT 0,1";
$result = mysql_query($query);
$num = mysql_num_rows($result);

if($num == 0) {
	echo "No new jobs\n";
}

while($row = mysql_fetch_array($result))
{
	echo "Processing document ID " . $row['id'] . "\n";
	//start processing task here
	$id = $row['id'];

	$temp = explode(".",$row['filename']);
	$arg0 = $row['id'] . " " . $row['filename'];
	$arg1 = $row['id'] . " " . $temp[0] . ".pdf";
	$torun = "sh script/converter.sh " . $arg0;
	$output=`$torun`;
	echo $output;
	$getwidth = "sh script/getwidth.sh " . $arg1;
	$getheight = "sh script/getheight.sh " . $arg1;
	$getpages = "sh script/getpages.sh " . $arg1;

	$output1 = `$getwidth`;
	echo "Width of PDF is " . $output1 . "\n";
	$output2 = `$getheight`;
	echo "Height if PDF is " . $output2 . "\n";
	$output3 = `$getpages`;
	echo "Total num of pages in PDF is " . $output3 . "\n";

	//insert into database
	$insertquery = "UPDATE documents SET status='1', width='$output1', height='$output2',pages='$output3' WHERE id='$id'";
	mysql_query($insertquery);

}


?>
